package com.faker.controller;

import com.faker.core.dto.GenerationRequest;
import com.faker.core.dto.ResponseResult;
import com.faker.core.jdbc.DataSourceChange;
import com.faker.service.DataGenerateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 数据生成 控制器
 */
@RestController
@RequestMapping(value = "dataGenerate")
@Slf4j
public class DataGenerateController {
    @Resource
    private DataGenerateService dataGenerateService;

    /**
     * 开始数据生成
     *
     * @param generationRequest
     * @return
     */
    @PostMapping(value = "/startGeneration")
    public ResponseResult startGeneration(@RequestBody GenerationRequest generationRequest) {
        DataSourceChange.changeDataSource();
        //全局变量参数
        List<Map<String, Object>> globalParams = dataGenerateService.getGlobalParams(generationRequest);

        Long connectionId = generationRequest.getConnectionId();
        DataSourceChange.changeDataSource(connectionId);
        //开始生成数据
        int completedNum = dataGenerateService.startGeneration(generationRequest, globalParams);
        return ResponseResult.success(completedNum);

    }

}
